package tool;

import bean.ModelType;
import com.google.common.collect.Lists;
import com.sun.istack.NotNull;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.mortbay.util.StringUtil;

import java.io.IOException;
import java.util.*;

import static tool.TableProperty.*;

public class UIEventHandler {
    private static UIEventHandler instance;

    private UIEventHandler() {
    }

    public static UIEventHandler getInstance() {
        if (instance == null) {
            instance = new UIEventHandler();
        }
        return instance;
    }

    private Map<String,Put> cacheMap = new HashMap<>();
    public void Put(String rowkey, String col, String value)
    {
        Put cache;
        if (!cacheMap.containsKey(rowkey))
        {
            cache = new Put(Bytes.toBytes(rowkey));
            cacheMap.put(rowkey,cache);
//            Lg.info(cacheMap.keySet());
//            Lg.info(cacheMap.values());
        }else
        {
            cache = cacheMap.get(rowkey);
//            Lg.info(cacheMap.keySet());
//            Lg.info(cacheMap.values());
        }
        cache.addColumn(Bytes.toBytes(COLFAM),Bytes.toBytes(col),Bytes.toBytes(value));
    }
    public void PutSC(@NotNull String sid,@NotNull String cid,@NotNull String score,@NotNull String tid)
    {
        if (tid == null)
        {
            tid = "";
        }
        sid = sid.replaceAll("[a-zA-Z].","");
        cid = cid.replaceAll("[a-zA-Z].","");
        tid = tid.replaceAll("[a-zA-Z].","");

        if (score == null)
        {
            score = "";
        }
        Put(SC+sid,ID,sid);
        Put(SC+sid,cid+"_"+tid,score);
        Put(SC+sid,TYPE,SC);
        Save();
    }
    private List<Delete> cacheDelList = new LinkedList<>();
    public void Delete(String rowkey)
    {
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        cacheDelList.add(delete);
    }

    public void Delete(String rowkey,String col)
    {
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        delete.addColumn(Bytes.toBytes(COLFAM),Bytes.toBytes(col));
        cacheDelList.add(delete);
    }
    public int Save()
    {
        ArrayList<Put> puts = Lists.newArrayList(cacheMap.values());
        int size = puts.size();
        Observable.just(puts).subscribeOn(Schedulers.computation())
                .observeOn(Schedulers.io())
                .subscribe(new Consumer<ArrayList<Put>>() {
                    @Override
                    public void accept(ArrayList<Put> puts) throws Exception {
                        HBaseTool.GetInstance().insertRow(TABLE_NAME,puts);
                        cacheMap.clear();
                    }
                });
        return size;
    }
    public void Reset(ModelType modelType)
    {
        cacheMap.clear();
    }
    public void SaveDel()
    {
        try {
            HBaseTool.GetInstance().deleRow(TABLE_NAME,cacheDelList);
            ResetDel();
        } catch (IOException e) {
            Lg.debug("删除失败");
        }
    }
    public void ResetDel()
    {
        cacheDelList.clear();
    }

    public void Check()
    {
        try {
            HBaseTool.GetInstance().createTable(TABLE_NAME, DataProvider.parse(COLFAM));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
